// components/kcb/kcb.js
const app = getApp();
var rapi = require('../../_self/api.js');
const sel_item = app.sel_item
var pm = require('../../_self/premap.js');
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 生命周期函数
   */
  lifetimes: {
    created(options) {
      var _ = this;
      
     // console.log(app.globalData.StatusBar)
      var xh = app.xh;
      this.RqClass();
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    xqzc: null,
    color: 'cyan',
    classes:[],
    zc:''
  },
//组件生命周期
  lifetimes:{
    created(){
      var _ = this; //Red
      wx.showLoading({
        title: '渲染~',
      })
      rapi.getCurrentTime(sel_item).then(res => {
        //console.log(res)
        if(res.result.token == '-1'){
          // 提示token 已过期并且提示刷新
        }
        //这里有必要的话可以加上对返回结果的预判
        _.setData({
          xqzc: res.result
        }, () => {
          _.RqClass();
        })
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    zcChange(e){
      wx.showLoading({
        title: '渲染~',
      })
      var _ = this;
      //获取点击方向
      var dir = e.currentTarget.dataset.direction;
      console.log(dir)
      var txqzc = _.data.xqzc;
      console.log('第'+txqzc.zc+'周')
      if(dir=='left'){
        txqzc.zc = txqzc.zc - 1;
        _.setData({
          xqzc:txqzc
        },()=>{
          _.RqClass()
        })
      }else{
        txqzc.zc = txqzc.zc + 1;
        _.setData({
          xqzc:txqzc
        },()=>{
          _.RqClass();
        })
      }
    },
    RqClass() {
      var _ = this;
      rapi.getKbcxAzc(sel_item, _.data.xqzc, app.xh).then(_res => {
        console.log(_res)
        wx.hideLoading();
        if(_res.result.token=='-1'){
           // 提示token 已过期并且提示刷新
           
        }
        _.preClass(_res.result)
      })
    },
    preClass(classes) {
      //预处理课表数据
      var _ = this;
      var t = pm.PreClasses(classes);
      var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
      console.log(t)
      if(t==null){
        wx.showToast({
          title: '本周没课',
          icon:"none"
        })
      }
      _.setData({
        classes:_.data.classes.concat(t),
        zj:weekday[new Date().getDay()]
      })
    }
  }
  
})